
//设置html根目录字体大小
require(['jquery','../modules/pageSize','../js/jqScroller'],function($,pageSetSize){
	setRootFontSize(pageSetSize);
})

// 搜索页面效果
require(['jquery'],function($){
    //热门搜索赋值
    $('.hot-key a').each(function(){
        $(this).on('click',function(){
            $('.input-box input').val($(this).text());
        })
    });
    // 搜索取消value值
    $('.value-delete').click(function(){
        $(this).siblings().val('');
    });
    //清除历史搜索记录
    $('.search-history h3 a').click(function(){
        $('.history-list *').remove();
		_global.cacher.remove("history");
    });
})

// 重构页面
require(['jquery','../modules/popup'],function($,popup){
    var isAjax = false,isEnd = false,isClickEvent = true;
    var arr = $('.result-list li');
    var $goodsData = $('.input-box input').val();
	var _ycMarkArr = ["闪购", "促销", "套餐"];
    //console.log($goodsData);
    if($goodsData == ''){
        isAjax = true;
    }
    $(".empty-box").hide();
    var url,a1,b1;
    var localId = _localId,
        setId = _setId,
        state = '', //asc (从低到高) desc
        order_field='', //order_field saleSort，priceSort，commentSort;
        $page  = 1;
    // 首次加载
    searchAjaxList(localId,setId,$goodsData,$page,order_field,state);

    //搜索结果排序
    $('.result-sort a').click(function(){
        isEnd = false;
        counter = 0;
        var _self = $(this);
        order_field = _self.attr('id');
        $('.result-list ul div.text').remove();
        if($(this).hasClass('')){
            $(this).addClass('ascending').siblings().attr('class','');
        }
        if($(this).hasClass('ascending')){
            isClickEvent = false;
            $(this).removeClass('ascending').addClass('descending');
        }else if($(this).hasClass('descending')){
            isClickEvent = true;
            $(this).removeClass('descending').addClass('ascending');
        }
        if(isClickEvent==true){
            state = 'desc';
        }else{
            state = 'asc';
        }
        //console.log(order_field);
        searchAjaxList(localId,setId,$goodsData,$page,order_field,state);
    });

    /*监听加载更多*/
    var counter = 0; /*计数器*/
    var pageStart = 0; /*offset*/
    var pageSize = 10; /*size*/
    $(window).scroll(function(){
        //console.log(isEnd, isAjax);
        /*滚动加载时如果已经没有更多的数据了、正在发生请求时，不能继续进行*/
        if(isEnd == true || isAjax == true){
            return;
        }
        // 当滚动到最底部以上100像素时， 加载新内容
        if ($(document).height() - $(this).scrollTop() - $(this).height()<100){
            counter ++;
            pageStart = counter * pageSize;
            searchAjaxList(localId,setId,$goodsData,(counter+$page),order_field,state);
        }
    });
    /**
     *  搜索商品 Ajax 请求更新数据 并且下拉刷新
     *  @searchAjaxList ajax请求方法
     *  @demoScrollEvenet 监听demo滚动
     */
    
    
    /*{:U('', ['order_field'=>'price', 'order'=>$order, 'p'=>$p], '')}?keywords={$keywords}*/
    //function demoScrollEvenet(){}

    function searchAjaxList(localId,setId,_store_pid,page,desc,order_field){
        var data = {
                keywords:_store_pid,
                p:page,
                order_field: desc,
                order: order_field
            },
            _html = '',
            res_cur='';
        if(setId ==''){
            url ='/Home/Search/index/local_id/'+localId+'' ;
        }
        else{
            url ='/Home/Search/index/local_id/'+localId+'/hotKey/'+setId+'';
        }
        isAjax = true;
        $.ajax({
            type:'get',
            url:url,
            data:data,
            dataType:'json',
            beforeSend: function () {
                $("body").append('<div id="pload" style="position:fixed;top:0;z-index:1200;background: rgba(0,0,0,0.3);width:100%;height:100%;margin:auto auto;">' +
                    '<p style="width:2.0rem;height: 2.0rem; margin: 0 auto;text-align: center; position: relative; top: 5rem;">' +
                    '<span style="font-size: .32rem;color: #3c6a63;">加载中...</span>' +
                    '<img src="/Public/Home/images/loading-more.gif" style="display: block;width: .32rem;height: .32rem; margin: 0 auto;"></p>'+
                    '</div>');
            },
            success:function(rs){
                isAjax = false;
                var _list = rs.data.list,
                    page = rs.data.p,
                    totalPage = rs.data.totalPage;
                if(_list == null){
                    $(".empty-box").show();
                    isAjax = true;
                    $('.result-list ul').remove(res_cur);
                }
                if(_list){
                    var sum = rs.data.list.length;
                    for(var i =0; i < sum; i++ ){
                        var store_cur = _list[i].store, store_last = _list[i].freez;
                        var store_out = parseInt(store_cur - store_last);
						// 0: 闪购 1：促销 2：组合促销
						var g_mark = -1;
						var g_price = "";
						if(!!_list[i].flashPrice){
							g_mark = 0;
							g_price = _list[i].flashPrice;								
						}else if(!!_list[i].proPrice){
							g_mark = 1;
							g_price = _list[i].proPrice;									
						}else if(_list[i].groupPromotion){
							g_mark = 2;
						}
                        _html +='<li>';
                        if(store_out > 0){
                            _html +='<a href="'+_list[i].url+'" class="img "><img src="'+getDefaultImg(_list[i].img)+'" alt=""></a>';
                        }
                        else {
                            _html +='<a href="'+_list[i].url+'" class="img sold_out"><img src="'+getDefaultImg(_list[i].img)+'" alt=""></a>';
                        }
                        _html +='<div class="text">';
                        _html +='<a href="'+_list[i].url+'" class="name">'+(g_mark == -1 ? "" : ('<i class="o-mark">'+_ycMarkArr[g_mark]+'</i>'))+_list[i].name+'</a>';
                        if(_list[i].spec_info != null){
                            _html +='<p class="des text-hide">'+_list[i].spec_info+'</p>';
                        }
						if(g_mark == 0){
							_html += '<p class="price"><span class="sale-price">￥'+g_price+'</span></p>';
						}else if(g_mark == 1 && parseFloat(g_price) != parseFloat(_list[i].price)){
							_html += '<p class="price"><span class="sale-price">￥'+g_price+'</span><span class="old-price">￥'+_list[i].price+'</span></p>';
						}else {
							_html += '<p class="price"><span class="sale-price">￥'+_list[i].price+'</span></p>';
						}
                        _html += '<p class="label">';
                        _html += '<span><label id="shopingNumber">'+_list[i].sales_num+'</label>人付款</span>';
                        _html += '<span><label id="commentScore">'+_list[i].grade+'</label>评分</span>';
                        _html += '<!--<span>免运费</span>-->';
                        _html += '</p>';
                        _html += '</div>';
                        _html += '</li>';
                    }
                }
                if(desc != ''){
                    if(parseInt(page) >1){
                        $('.result-list ul').append(_html);
                    }
                    else {
                        $('.result-list ul').html(_html);
                    }
                }
                else {
                    $('.result-list ul').append(_html);
                }
                if ( parseInt(page)  >= totalPage){
                    res_cur +='<div class="text" style="font-size: .26rem; padding:.15rem 0; text-align: center;line-height: 1.5;color:#787878; ">已经显示完全所有商品</div>';
                    $('.result-list ul').append(res_cur);
                    isEnd = true;
                }
            },
            complete: function () {
                $("#pload").remove();
            },
            error:function(){
                popup.alertPopup("服务器异常,请稍后重试！");
            }
        })
    }

})